package com.zisuyi.game.sdk.base

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.viewbinding.ViewBinding
import com.weilele.mvvm.base.helper.createViewBinding
import com.weilele.mvvm.utils.activity.getResColor
import com.weilele.mvvm.utils.activity.getResDrawable
import com.zisuyi.game.sdk.library.R
import java.lang.IllegalArgumentException

class BaseHelperImpl<Binding : ViewBinding?> : IBaseHelperView<Binding> {

    private var _binding: Binding? = null

    override val mBinding: Binding
        get() {
            return _binding ?: throw IllegalArgumentException("binding is null")
        }

    private val baseHelperBean = IBaseHelperView.BaseHelperBean(null, null, null)

    private fun getToolbar(context: Context, isNeedToolbar: Boolean): Toolbar? {
        return if (isNeedToolbar) {
            Toolbar(context).apply {
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
            }
        } else {
            return null
        }
    }

    private lateinit var self: Any
    override fun getContentView(
        self: Any,
        context: AppCompatActivity,
        layoutInflater: LayoutInflater,
        parent: ViewGroup?,
        isNotActivity: Boolean,
        isNeedToolbar: Boolean,
        isNeedBackIcon: Boolean,
        isNeedSwitchView: Boolean
    ): Any? {
        this.self = self
        val toolbar = getToolbar(context, isNeedToolbar)
        val switcherView = getSwitchView(context, layoutInflater, isNeedSwitchView)
        baseHelperBean.toolbar = toolbar
        baseHelperBean.switcherView = switcherView
        return if (toolbar == null) {
            switcherView ?: createBindingView(layoutInflater, parent)
        } else {
            //toolbar不是null，
            //判断是在activity还是fragment添加
            addToolbarView(context, layoutInflater, parent, toolbar, switcherView, isNotActivity)
                .apply { initDefaultToolbarUi(context, toolbar, isNeedBackIcon) }
        }
    }

    private fun createBindingView(inflater: LayoutInflater, parent: ViewGroup?): View? {
        val binding = _binding ?: self.createViewBinding(inflater, parent = parent)
        _binding = binding
        return binding?.root
    }

    private fun addToolbarView(
        context: AppCompatActivity, layoutInflater: LayoutInflater,
        parent: ViewGroup?,
        toolbar: Toolbar,
        switcherView: SwitchView?,
        isNotActivity: Boolean
    ): Any? {
        return if (isNotActivity) {
            //不是activity的view
            addToolbarViewInNotActivity(context, layoutInflater, toolbar, switcherView)
        } else {
            //在activity中
            addToolbarViewInActivity(context, layoutInflater, parent, toolbar, switcherView)
        }
    }

    /**
     * 在activity中添加toolbar
     */
    private fun addToolbarViewInActivity(
        context: AppCompatActivity,
        layoutInflater: LayoutInflater,
        parent: ViewGroup?,
        toolbar: Toolbar,
        switcherView: SwitchView?
    ): Any? {
        return if (context.supportActionBar != null) {
            //已经有了actionbar
            switcherView ?: createBindingView(layoutInflater, parent)
        } else {
            //没有actionbar
            context.setSupportActionBar(toolbar)
            addToolbarViewInNotActivity(context, layoutInflater, toolbar, switcherView)
        }
    }

    /**
     * 在fragment中添加toolbar
     */
    private fun addToolbarViewInNotActivity(
        context: AppCompatActivity,
        layoutInflater: LayoutInflater,
        toolbar: Toolbar,
        switcherView: SwitchView?,
    ): Any {
        val rootView = LinearLayout(context)
        rootView.id = View.generateViewId()
        baseHelperBean.rootView = rootView
        rootView.orientation = LinearLayout.VERTICAL
        rootView.addView(toolbar)
        if (switcherView != null) {
            rootView.addView(switcherView)
        } else {
            rootView.addView(createBindingView(layoutInflater, rootView))
        }
        return rootView
    }

    private fun getSwitchView(
        context: Context,
        layoutInflater: LayoutInflater,
        isNeedSwitchView: Boolean
    ): SwitchView? {
        return if (isNeedSwitchView) {
            SwitchView(context) {
                createBindingView(layoutInflater, it) ?: View(it.context)
            }.apply {
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            }
        } else {
            null
        }
    }

    /**
     * 初始化标题栏外观
     */
    private fun initDefaultToolbarUi(
        context: AppCompatActivity,
        toolbar: Toolbar,
        isNeedBackIcon: Boolean
    ) {
        toolbar.setBackgroundResource(R.color.colorPrimary)
        if (isNeedBackIcon) {
            val drawable = context.getResDrawable(R.drawable.mvvm_icon_back)
            drawable?.setTint(context.getResColor(R.color.textColorPrimary))
            toolbar.navigationIcon = drawable
            toolbar.title = context.title
            toolbar.setNavigationOnClickListener { context.onBackPressed() }
        }
    }

    override fun getBaseHelperBean(): IBaseHelperView.BaseHelperBean {
        return baseHelperBean
    }

    override fun getToolbar(): Toolbar? = baseHelperBean.toolbar

    override fun getSwitchView(): SwitchView? = baseHelperBean.switcherView

    override fun getRootView(): LinearLayout? = baseHelperBean.rootView
}